package com.blog.controller;

import com.blog.dao.TagDao;
import com.blog.pojo.Article;
import com.blog.pojo.Tag;
import com.blog.service.ArticleService;
import com.blog.service.TagService;
import com.blog.service.impl.ArticleServiceImpl;
import com.blog.service.impl.TagServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(value = "/GoEditArticleServlet.do")
public class GoEditArticleServlet extends HttpServlet {
    ArticleService articleService =new ArticleServiceImpl();
    TagService tagService=new TagServiceImpl();
    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        //把文章id获取
        String id = req.getParameter("articleId");
        //通过id查到文章
        Article article =new Article();
        article=articleService.getArticlesById(id);

        //加载文章标签
        List<Tag> articleTagList = tagService.getTagsById(id);
        String articleTag="";
        for (Tag tag : articleTagList) {
            articleTag=tag.getTag()+" ";
        }
        req.setAttribute("articleTag",articleTag);

        //存储到作用域，转发到编辑jsp页面
        req.setAttribute("article",article);

        //加载所有的博客分类分类
        Map<String, Integer> sort_count = articleService.getArticleSortAndCount();
        req.setAttribute("sort_count",sort_count);

        //加载标签
        List<Tag> all_tag = tagService.getTags();
        req.setAttribute("all_tag",all_tag);

        System.out.println(article);
        req.getRequestDispatcher("/admin/edit.jsp").forward(req, res);
    }
}
